<?php
namespace app\index\controller;
use think\Controller;
use app\index\controller\Common;

class Index extends Common
{
    public function index()
    {
		$this->doindex();
		$list  = db('school')->group('type')->field("type as id,CASE type WHEN 1 THEN '自学考试' WHEN 2 THEN '成人高考' WHEN 3 THEN '网教教育' WHEN 4 THEN '国开电大' ELSE '同等学力申硕' END AS title,CASE type WHEN 1 THEN 'zxks' WHEN 2 THEN 'crgk' WHEN 3 THEN 'wljy' WHEN 4 THEN 'gkdd' ELSE 'tdxlss' END AS url")->select(); 
		foreach ($list as $key => $value) {
			$list[$key]['child'] = db('gradation')->alias('a')->group('a.level')->join('school b','a.school_id = b.id')->where('b.type',$value['id'])->field("a.level as id ,CASE a.level WHEN 1 THEN '高起专' WHEN 2 THEN '专升本' WHEN 3 THEN '专科专业' ELSE '本科专业' END AS title")->select();
			foreach ($list[$key]['child'] as $k => $val) {
				$list[$key]['child'][$k]['child'] = db('gradation')->where('level',$val['id'])->field('id,professional_title as title')->limit(8)->select();
			}
			if($value['id'] == 4){//国开电大
				$list[$key]['college'] = db('school')->where('id',12)->field('id,school_thumb')->limit(1)->select();
			}else{
				$list[$key]['college'] = db('school')->where('type',$value['id'])->field('id,school_thumb')->limit(4)->select();
			}
		}
		
		//招生院校
		$type  = db('school')->group('type')->field("type as id,CASE type WHEN 1 THEN '自学考试' WHEN 2 THEN '成人高考' WHEN 3 THEN '网教教育' WHEN 4 THEN '国开电大' ELSE '同等学力申硕' END AS title,CASE type WHEN 1 THEN 'zxks' WHEN 2 THEN 'crgk' WHEN 3 THEN 'wljy' WHEN 4 THEN 'gkdd' ELSE 'tdxlss' END AS url")->select(); 
		foreach ($type as $key => $value) {
			if($value['id'] == 4){
				$type[$key]['child'] = db('school')->where('type',$value['id'])->limit(1)->field('id,school_name')->select();
			}else{
				$type[$key]['child'] = db('school')->where('type',$value['id'])->limit(4)->field('id,school_name')->select();
			}
		}
		//print_r($type);exit;
		//推荐专业
		$recommended = db('gradation')->alias('a')->where('a.hot',1)->join('school b','a.school_id = b.id')->field('a.id,a.professional_title,a.professional_logo,b.school_name')->limit(5)->select();
		//热门专业
		$hot = db('gradation')->alias('a')->where('a.hot',2)->join('school b','a.school_id = b.id')->field('a.id,a.professional_title,a.professional_logo,b.school_name')->limit(5)->select();
		//合作学院
		$cooperationSchool = db('school')->order('id','desc')->where('id','<>',12)->field('id,school_name,label,level,school_logo')->select();
		foreach ($cooperationSchool as $key => $value) {
            $cooperationSchool[$key]['label'] =  implode('/',db('label')->whereIn('id',$value['label'])->column('title'));
			$cooperationSchool[$key]['level'] = conversion($value['level']);
        }
		//最新资讯
		$article = db('article')->alias('a')->order('a.id','desc')->where('a.cid','>=',1)->join('cate b','a.cid = b.id')->field('a.id,a.notice,a.title,a.datetime,b.entitle')->limit(8)->select();
		$this->assign("recommended",$recommended);
		$this->assign("article",$article);
		$this->assign("cooperationSchool",$cooperationSchool);
		$this->assign("hot",$hot);
		$this->assign("list",$list);
		$this->assign("type",$type);
		$this->combination();
		return $this->fetch('index@'.config('webthemes').'/index'); 
    }	
	public function doindex(){
		$this->assign("site",getseo());
		$this->assign("nid",0);
	}	
	public function combination(){
		/*
			自学考试=====================================================================================================================================================================
		*/
		$zxks['school'] = db('school')->where('type',1)->field('id,school_name,school_thumb')->limit(6)->select();
		//自考专业
		$zxks['professional'] = db('gradation')->alias('a')->join('school b','a.school_id = b.id')->where('b.type',1)->field('a.id,a.professional_title,a.professional_logo')->limit(6)->select();
		//资讯
		$zxks['information'] = db('article')->where('cid',105)->field('id,title,datetime,notice')->limit(8)->select();
		//学院排名
		$zxks['xy_ranking'] = db('school')->order('number','desc')->where('type',1)->field('id,school_name,school_thumb')->field('id,school_name')->limit(6)->select();
		//专业排名
		$zxks['zy_ranking'] = db('gradation')->alias('a')->join('school b','a.school_id = b.id')->where('b.type',1)->order('b.number','desc')->limit(10)->field('a.id,a.professional_title')->select();
		
		/*
			成人高考=====================================================================================================================================================================
		*/
		$crgk['school'] = db('school')->where('type',2)->field('id,school_name,school_thumb')->limit(6)->select();
		//自考专业
		$crgk['professional'] = db('gradation')->alias('a')->join('school b','a.school_id = b.id')->where('b.type',2)->field('a.id,a.professional_title,a.professional_logo')->limit(6)->select();
		//资讯
		$crgk['information'] = db('article')->where('cid',21)->field('id,title,datetime,notice')->limit(8)->select();
		//学院排名
		$crgk['xy_ranking'] = db('school')->order('number','desc')->where('type',2)->field('id,school_name,school_thumb')->field('id,school_name')->limit(6)->select();
		//专业排名
		$crgk['zy_ranking'] = db('gradation')->alias('a')->join('school b','a.school_id = b.id')->where('b.type',2)->order('b.number','desc')->limit(10)->field('a.id,a.professional_title')->select();

		/*
			网络教育=====================================================================================================================================================================
		*/
		
		$wljy['school'] = db('school')->where('type',3)->field('id,school_name,school_thumb')->limit(6)->select();
		//自考专业
		$wljy['professional'] = db('gradation')->alias('a')->join('school b','a.school_id = b.id')->where('b.type',3)->field('a.id,a.professional_title,a.professional_logo')->limit(6)->select();
		//资讯
		$wljy['information'] = db('article')->where('cid',106)->field('id,title,datetime,notice')->limit(8)->select();
		//学院排名
		$wljy['xy_ranking'] = db('school')->order('number','desc')->where('type',3)->field('id,school_name,school_thumb')->field('id,school_name')->limit(6)->select();
		//专业排名
		$wljy['zy_ranking'] = db('gradation')->alias('a')->join('school b','a.school_id = b.id')->where('b.type',3)->order('b.number','desc')->limit(10)->field('a.id,a.professional_title')->select();
		
		/*
			国开电大=====================================================================================================================================================================
		*/
		$gkdd['school'] = db('school')->where('type',4)->field('id,school_name,school_thumb')->limit(6)->select();
		//自考专业
		$gkdd['professional'] = db('gradation')->alias('a')->join('school b','a.school_id = b.id')->where('b.type',4)->field('a.id,a.professional_title,a.professional_logo')->limit(6)->select();
		//资讯
		$gkdd['information'] = db('article')->where('cid',107)->field('id,title,datetime,notice')->limit(8)->select();
		//专业排名
		$gkdd['zy_ranking'] = db('gradation')->alias('a')->join('school b','a.school_id = b.id')->where('b.type',4)->order('b.number','desc')->limit(10)->field('a.id,a.professional_title')->select();
		
		/*
			同等学力申硕=====================================================================================================================================================================
		*/
		$tdxlss['school'] = db('school')->where('type',5)->field('id,school_name,school_thumb')->limit(6)->select();
		//自考专业
		$tdxlss['professional'] = db('gradation')->alias('a')->join('school b','a.school_id = b.id')->where('b.type',5)->field('a.id,a.professional_title,a.professional_logo')->limit(6)->select();
		//资讯
		$tdxlss['information'] = db('article')->where('cid',114)->field('id,title,datetime,notice')->limit(8)->select();
		//学院排名
		$tdxlss['xy_ranking'] = db('school')->order('number','desc')->where('type',5)->field('id,school_name,school_thumb')->field('id,school_name')->limit(6)->select();
		//专业排名
		$tdxlss['zy_ranking'] = db('gradation')->alias('a')->join('school b','a.school_id = b.id')->where('b.type',5)->order('b.number','desc')->limit(10)->field('a.id,a.professional_title')->select();
		
		
		
		
		
		$this->assign("combination",['zxks'=>$zxks,'crgk'=>$crgk,'wljy'=>$wljy,'gkdd'=>$gkdd,'tdxlss'=>$tdxlss]);
		
	}
		
}
